import { IUser } from '@/types/user';
import { ParamsType } from '@ant-design/pro-components';
import { request } from 'umi';

/**
 * 分页获取用户信息
 */

function getUserByPage(
  params: ParamsType & {
    pageSize?: number;
    current?: number;
    keyword?: string;
  },
) {
  return request('/api/user', {
    method: 'GET',
    params: {
      ...params,
    },
  });
}

/**
 * 根据用户 id 获取用户详情
 */

function getUserById(userId: string) {
  return request(`/api/user/${userId}`, {
    method: 'GET',
  });
}

/**
 * 新增用户
 */
function addUser(newUserInfo: IUser) {
  return request('/api/user', {
    method: 'POST',
    data: { ...newUserInfo, type: 'background' },
  });
}

/**
 * 根据 id 删除用户
 */
function deleteUser(userId: string) {
  return request(`/api/user/${userId}`, {
    method: 'DELETE',
  });
}

/**
 * 根据 id 修改用户
 */
function editUser(userId: string, newUserInfo: IUser) {
  return request(`/api/user/${userId}`, {
    method: 'PATCH',
    data: newUserInfo,
  });
}

/**
 * 查询用户是否存在
 */
export function userIsExist(loginId: string) {
  return request(`/api/user/userIsExist/${loginId}`, { method: 'GET' });
}

export default {
  getUserByPage,
  getUserById,
  addUser,
  deleteUser,
  editUser,
  userIsExist,
};
